/* === This file is part of Calamares - <https://calamares.io> ===
 *
 *   SPDX-FileCopyrightText: 2014-2015 Teo Mrnjavac <teo@kde.org>
 *   SPDX-FileCopyrightText: 2017 Adriaan de Groot <groot@kde.org>
 *   SPDX-License-Identifier: GPL-3.0-or-later
 *
 *   Calamares is Free Software: see the License-Identifier above.
 *
 */

#include "ProgressTreeDelegate.h"

#include "Branding.h"
#include "CalamaresApplication.h"
#include "CalamaresWindow.h"
#include "ViewManager.h"
#include "utils/Gui.h"
#include "utils/Logger.h"

#include <QPainter>

static constexpr int const item_margin = 8;
static inline int
item_fontsize()
{
    return Calamares::defaultFontSize();
}
static bool contains(const std::vector<QString>& vec,const QString& value){
  for(const QString& v : vec){
    if(v==value){
      return true;
    }
  }
  return false;
}
static QString getTypeOfStep(const QString& stepString){
  std::vector<QString> baseSetting1 = {"Location","Keyboard"};
  std::vector<QString> userSetting1 = {"Users","Packages"};
  std::vector<QString> installSteps1 = {"Partitions","Summary","Install","Finish"};
  std::vector<QString> baseSetting2 = {"位置","键盘"};
  std::vector<QString> userSetting2 = {"用户","桌面"};
  std::vector<QString> installSteps2 = {"分区","摘要","安装","结束"};
  
  if(contains(baseSetting1,stepString)){
    return "BaseSetting";
  }
  if(contains(userSetting1,stepString)){
    return "UserSetting";
  }

  if(contains(installSteps1,stepString)){
    return "InstallSetting";
  }
  if(contains(baseSetting2,stepString)){
    return "基础设置";
  }
  if(contains(userSetting2,stepString)){
    return "用户设置";
  }

  if(contains(installSteps2,stepString)){
    return "安装设置";
  }

  return 0;
}
static int getTypeOfStep(int val){
  if(val<=1){
    return 1;
  }
  else if(val<=3){
    return 2;
  }
  else if(val<=8){
    return 3;
  }
  else{
    return 0;
  }
}
static bool isVisable(const QString& stepString){
  if(stepString=="Keyboard"||stepString=="Packages"||stepString=="Summary"||stepString=="Install"||stepString=="Finish"||
      stepString=="键盘"||stepString=="桌面"||stepString=="摘要"||stepString=="安装"||stepString=="结束"){
    return false;
  }
  return true;
}
static void
paintViewStep( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index )
{
    QRect textRect = option.rect.adjusted( item_margin, item_margin, -item_margin, -item_margin );
    QFont font = qApp->font();
    font.setPointSize( item_fontsize() );
    font.setBold( false );
    painter->setFont( font );
     
    int CurrentTypeOfStep = getTypeOfStep(index.data( Calamares::ViewManager::ProgressTreeItemCurrentIndex ).toInt());
    int TypeOfStep =  getTypeOfStep(index.row());
    if ( CurrentTypeOfStep == TypeOfStep )
    {
        painter->setPen( Calamares::Branding::instance()->styleString( Calamares::Branding::SidebarTextCurrent ) );
        QString textHighlight
            = Calamares::Branding::instance()->styleString( Calamares::Branding::SidebarBackgroundCurrent );
        if ( textHighlight.isEmpty() )
        {
            painter->setBrush( CalamaresApplication::instance()->mainWindow()->palette().window() );
        }
        else
        {
            painter->setBrush( QColor( textHighlight ) );
        }
    }

    // Draw the text at least once. If it doesn't fit, then shrink the font
    // being used by 1 pt on each iteration, up to a maximum of maximumShrink
    // times. On each loop, we'll have to blank out the rectangle again, so this
    // is an expensive (in terms of drawing operations) thing to do.
    //
    // (The loop uses <= because the counter is incremented at the start).
    static constexpr int const maximumShrink = 4;
    int shrinkSteps = 0;
    do
    {
     
        shrinkSteps++;

        QRectF boundingBox;
        cDebug() << "index.data().toString()" << index.data().toString();
        QString text;
        if(index.data().toString()=="欢迎" || index.data().toString()=="Welcome"||!isVisable(index.data().toString())){
            text = "";
        }else{
            text = getTypeOfStep(index.data().toString());
            painter->fillRect( option.rect, painter->brush().color() );
        }
        painter->drawText(
            textRect, Qt::AlignHCenter | Qt::AlignVCenter | Qt::TextSingleLine, text, &boundingBox );

        // The extra check here is to avoid the changing-font-size if we're not going to use
        // it in the next iteration of the loop anyway.
        if ( ( shrinkSteps <= maximumShrink ) && ( boundingBox.width() > textRect.width() ) )
        {
            font.setPointSize( item_fontsize() );
            painter->setFont( font );
        }
        else
        {
            break;  // It fits
        }
    } while ( shrinkSteps <= maximumShrink );
}

QSize
ProgressTreeDelegate::sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
    if ( !index.isValid() )
    {
        return option.rect.size();
    }

    QFont font = qApp->font();
    cDebug() << "ProgressTreeDelegate::sizeHint";

    font.setPointSize( item_fontsize() );
    QFontMetrics fm( font );
    int height = fm.height();
    height += 2 * item_margin;
    if(index.data().toString()=="欢迎" || index.data().toString()=="Welcome"||!isVisable(index.data().toString())){
        return QSize( 0, 0 );
    }else{
        return QSize( (Calamares::windowMinimumWidth*0.9)/3, height*2 );
    }
//    return QSize( option.rect.width(), height );
    
}

void
ProgressTreeDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
    cDebug() << "ProgressTreeDelegate::paint----------";
    QStyleOptionViewItem opt = option;

    painter->save();

    initStyleOption( &opt, index );
    opt.text.clear();

    painter->setBrush(
        QColor( Calamares::Branding::instance()->styleString( Calamares::Branding::SidebarBackground ) ) );
    painter->setPen( QColor( Calamares::Branding::instance()->styleString( Calamares::Branding::SidebarText ) ) );
    if(index.data().toString()=="欢迎" || index.data().toString()=="Welcome"){
        cDebug() << "ProgressTreeDelegate::欢迎Welcome--------Nopaint----------";
    }else{
            paintViewStep( painter, opt, index );
    }
    //paintViewStep( painter, opt, index );

    painter->restore();
}
